package org.spring.springboot.app.community.qruserinfo.controller;


import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.spring.springboot.app.base.Constants;
import org.spring.springboot.app.base.R;
import org.spring.springboot.app.base.User;
import org.spring.springboot.app.community.info.domain.vo.CommunityInfoInsertAndUpadteVO;
import org.spring.springboot.app.community.member.domain.vo.ExcelCommunityMemberVO;
import org.spring.springboot.app.community.qruserinfo.domain.po.QrUserInfoPO;
import org.spring.springboot.app.community.qruserinfo.domain.vo.QrUserInfoExcelVO;
import org.spring.springboot.app.community.qruserinfo.domain.vo.QrUserInfoSelectParamVO;
import org.spring.springboot.app.community.qruserinfo.domain.vo.QrUserInfoSelectVO;
import org.spring.springboot.app.community.qruserinfo.service.IQrUserInfoService;
import org.spring.springboot.app.community.structureic.domain.po.HardIcPO;
import org.spring.springboot.app.community.structureic.service.ICommunityStructureIcService;
import org.spring.springboot.app.sys.office.domain.po.SysOfficePO;
import org.spring.springboot.app.sys.office.service.ISysOfficeService;
import org.spring.springboot.app.sys.systemoption.service.ISystemOptionService;
import org.spring.springboot.component.annotation.AccessTokenVerify;
import org.spring.springboot.util.ApiIndex;
import org.spring.springboot.util.DateUtils;
import org.spring.springboot.util.excel.ExportExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

@Api(description="社区-防丢二维码管理",tags = ApiIndex.QrUserInfoController)
@RequestMapping(value = "/api/QrUserInfo")
@RestController
public class QrUserInfoController {

    @Autowired
    private IQrUserInfoService qrUserInfoService;
    @Autowired
    private ISysOfficeService sysOfficeService;
    @Autowired
    private ISystemOptionService systemOptionService;

    @ApiOperation(value = "查询所有二维码-分页查询")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "/findPage", method = RequestMethod.GET)
    public R<PageInfo<List<QrUserInfoSelectVO>>> findPage(
            @ApiIgnore User user,
            @ApiParam(value = "用户名", required = false) @RequestParam(defaultValue = "") String userName,
            @ApiParam(value = "是否填充(0.未填充 1.已填充)", required = false) @RequestParam(defaultValue = "") String isFill,
            @ApiParam(value = "是否下载(0.未下载 1.已下载)", required = false) @RequestParam(defaultValue = "") String isLoad,
            @ApiParam(value = "创建时间-开始(格式：yyyy-MM-dd)", required = false) @RequestParam(defaultValue = "") String createDateBegin,
            @ApiParam(value = "创建时间-结束(格式：yyyy-MM-dd)", required = false) @RequestParam(defaultValue = "") String createDateEnd,
            @ApiParam(value = "页数", required = true) @RequestParam(defaultValue = "1") int pageNum,
            @ApiParam(value = "每页条数", required = true) @RequestParam(defaultValue = "10") int pageSize
    ) {
        R<PageInfo<List<QrUserInfoSelectVO>>> result = new R<PageInfo<List<QrUserInfoSelectVO>>>();
        List<QrUserInfoSelectVO> _list = qrUserInfoService.findPage(pageNum, pageSize, userName,isFill,isLoad,createDateBegin,createDateEnd,user);
        String url = systemOptionService.getString(Constants.TOW_CODE_URL);
        for (QrUserInfoSelectVO vo:_list) {
            vo.setUrl(url+vo.getId());
        }
        PageInfo p =new PageInfo(_list);
        p.setList(p.getList());
        result.setData(p);
        return result;
    }
    @ApiOperation(value = "购买二维码")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "", method = RequestMethod.POST)
    public R save(
            @ApiParam(value = "购买数量", required = true) @RequestParam(defaultValue = "0") int num,
            @ApiIgnore User user
    ) {
        R result = new R();
        if(num <= 0){
            result.setCode(1);
            result.setMessage("购买数量必须大于0");
            return result;
        }
        //余额判断
        String officeId=user.getOfficeId();
        SysOfficePO  office=sysOfficeService.find(officeId);
        Double price = systemOptionService.getDouble(Constants.TOW_CODE_PRICE);
        BigDecimal totalPrice=new BigDecimal(price*num+"");
        BigDecimal balance=new BigDecimal(office.getBalance()+"");

        if(balance.doubleValue()<totalPrice.doubleValue()){
            result.setCode(1);
            result.setMessage("代理商账户余额："+office.getBalance()+"元，购买金额："+totalPrice.toPlainString()+"元，还差："+(totalPrice.subtract(balance).toString())+"，请先充值!");
            return result;
        }
        int count=qrUserInfoService.save(num,officeId,user.getId(),totalPrice.toString());
        if(count!=0){
            result.setCode(1);
            result.setMessage("保存失败");
        }
        return  result;
    }
    @ApiOperation(value = "标记二维码")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "updateState", method = RequestMethod.PUT)
    public R update(
            @ApiParam(value = "二维码对象", required = true) @RequestBody QrUserInfoSelectParamVO vo,
            @ApiIgnore User user
    ) {
        R result = new R();
        if(!vo.getState().equals("0") && !vo.getState().equals("1")){
            result.setCode(1);
            result.setMessage("state状态必须为0.未下载  或  1.已下载!");
            return result;
        }
        int count=qrUserInfoService.updateBySelect(vo.getState(),user.getOfficeId(),vo.getUserName(),vo.getIsFill(),vo.getIsLoad(),vo.getCreateDateBegin(),vo.getCreateDateEnd());
        return  result;
    }
    @ApiOperation(value = "下载二维码")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "download", method = RequestMethod.GET)
    public R download(
            @ApiParam(value = "request", hidden = true) HttpServletRequest request,
            @ApiParam(value = "response", hidden = true) HttpServletResponse response,
            @ApiParam(value = "二维码对象", required = true) @RequestBody QrUserInfoSelectParamVO vo,
            @ApiIgnore User user
    ) {
        R result = new R();
        List<QrUserInfoExcelVO> list=qrUserInfoService.findBySelect(vo.getUserName(),vo.getIsFill(),vo.getIsLoad(),vo.getCreateDateBegin(),vo.getCreateDateEnd(),user);
        String url = systemOptionService.getString(Constants.TOW_CODE_URL);
        for (QrUserInfoExcelVO vo1:list) {
            vo1.setUrl(url+vo1.getId());
        }
        String fileName = "二维码路径数据.xlsx";
        try {
            ExportExcel e =  new ExportExcel("防丢二维码地址", QrUserInfoExcelVO.class).setDataList(list).write(response, fileName).dispose();
        } catch (Exception e) {
            return result.fail().setMessage("导出二维码失败！失败信息:"+e.getMessage());
        }
        return  result.success();
    }

}
